package library.controller;

import library.po.User;
import library.service.BookService;
import library.service.BorrowService;
import library.service.UserService;
import library.service.impl.BookServiceImpl;
import library.service.impl.BorrowServiceImpl;
import library.service.impl.UserServiceImpl;
import view.CURD.UpdatePasswordDialog;
import view.info.BorrowBooksDialog;
import view.info.PersonalInfoDialog;
import view.main.MainFrame;
import view.login.LoginDialog;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class FrameListener implements ActionListener {
    private MainFrame owner;
    private JDialog dialog;
    private JPasswordField used,renew;
    private User toolUser;
    private UserService userService = new UserServiceImpl();
    private BookService bookService = new BookServiceImpl();
    private BorrowService borrowService = new BorrowServiceImpl();
    private PersonalInfoDialog pid;

    public FrameListener(MainFrame owner,User user){
        this.owner = owner;
        this.toolUser = user;
    }
    public FrameListener(JDialog dialog, JPasswordField used,JPasswordField renew){
        this.dialog = dialog;
        this.used = used;
        this.renew = renew;
    }
    public void setPasswordField(JPasswordField used,JPasswordField renew){
        this.used = used;
        this.renew = renew;
    }
    public void setDialog(JDialog dialog){
        this.dialog = dialog;
    }
    public FrameListener(){
        super();
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        //退出
        if ("Exit".equals(e.getActionCommand())){
            owner.dispose();
        }
        //重新登陆
        else if ("ReLogin".equals(e.getActionCommand())){
            owner.dispose();
            new LoginDialog();
        }
        //修改密码
        else if ("UpdatePassword".equals(e.getActionCommand())){
            new UpdatePasswordDialog(owner);
        }
        else if ("PasswordUpdatePost".equals(e.getActionCommand())){
            if(this.checkPassword(used.getText().trim())&&used.getText().trim()!=renew.getText().trim()){
                toolUser.setUserPassword(renew.getText().trim());
                userService.updatePassword(toolUser);
                JOptionPane.showMessageDialog(dialog, "修改密码成功!","成功",JOptionPane.INFORMATION_MESSAGE);
                dialog.dispose();
            }
            else{
                JOptionPane.showMessageDialog(dialog, "请检查原密码是否正确，并且新密码不可与原密码相同。","修改密码失败",JOptionPane.ERROR_MESSAGE);
            }
        }
        else if ("PasswordCancel".equals(e.getActionCommand())){
            JOptionPane.showMessageDialog(dialog, "退出修改！","取消",JOptionPane.INFORMATION_MESSAGE);
            dialog.dispose();
        }
        //查看个人信息
        else if ("CheckPersonalInfo".equals(e.getActionCommand())){
            pid = new PersonalInfoDialog(owner,toolUser);
        }
        else if("InfoExit".equals(e.getActionCommand())){
            dialog.dispose();
        }
        //查看已借书籍
        else if("ViewBorrowBooks".equals(e.getActionCommand())){
            new BorrowBooksDialog(pid,toolUser);
        }
        //修改个人信息
        else if("UpdatePersonalInfo".equals(e.getActionCommand())){

        }
    }
    public boolean checkPassword(String password){
        if (password.equals(toolUser.getUserPassword())){return true;}
        else {return false;}
    }
    public String typeToIdentity(int type) {
        String str = null;
        if (type == 1) {
            str = "借阅者";
        }
        else if(type == 3){
            str = "图书管理员&借阅者";
        }
        else if(type == 2){
            str = "系统管理员&读者";
        }
        else if (type == 4)
            str = "系统管理员&图书管理员&借阅者";
        return str;
    }
}
